package de.pass.ch.junit;

import static org.junit.Assert.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.junit.*;

import de.pass.ch.graph.UpdateableGraph;
import de.pass.ch.io.GraphIO;
import de.pass.ch.pt.StreetProfile;
import de.pass.ch.util.Constants;

public class TestUpdateableGraph {

	private static final int DUMMY = 0;
	private static final int LITAUEN = 1;
	private static final int DACH = 2;
	private static final Runtime RUNTIME = Runtime.getRuntime();

	private static final int[] NODESIZES = { 10, 354437, 11997740 };

	// @Before
	// public void waitToStartVisualVM() {
	// try {
	// Thread.sleep(10 * 1000);
	// } catch (InterruptedException e) {
	// e.printStackTrace();
	// }
	// }

	@Test
	public void testUpdateableGraphDummy() {

		UpdateableGraph graph = GraphIO.importUpdatableGraph("dummyGraph", "",
				StreetProfile
						.getInstance(StreetProfile.DEFAULT_PROFILE),
				Constants.DISTANCE_OPT);
		assertEquals(NODESIZES[DUMMY], graph.noOfNodes());

		System.out.println(graph.noOfNodes() + " nodes");
		System.out.println(graph.noOfEdges() + " edges");

		try {

			File file = new File("D:\\dummyGraph.upg");
			if (!file.exists())
				file.createNewFile();
			FileOutputStream out = new FileOutputStream(file);
			graph.serialize(out);
			out.close();

			FileInputStream in = new FileInputStream(file);
			graph = UpdateableGraph.deserialize(in);
			in.close();

		} catch (IOException e) {
			e.printStackTrace();
		}

		assertEquals(NODESIZES[DUMMY], graph.noOfNodes());

		System.gc();

		System.out.println("used Memory: "
				+ (RUNTIME.totalMemory() - RUNTIME.freeMemory()) + " byte");

		graph = null;
		System.gc();

		System.out.println("\n");
	}

	@Test
	public void testUpdateableGraphLitauen() {

		UpdateableGraph graph = GraphIO.importUpdatableGraph("D:\\litauen", "",
				StreetProfile
						.getInstance(StreetProfile.DEFAULT_PROFILE),
				Constants.DISTANCE_OPT);
		assertEquals(NODESIZES[LITAUEN], graph.noOfNodes());

		System.out.println(graph.noOfNodes() + " nodes");
		System.out.println(graph.noOfEdges() + " edges");

		// try {
		//
		// File file = new File("D:\\litauen.upg");
		// if (!file.exists())
		// file.createNewFile();
		// FileOutputStream out = new FileOutputStream(file);
		// graph.serialize(out);
		// out.close();
		//
		// FileInputStream in = new FileInputStream(file);
		// graph = UpdateableGraph.deserialize(in);
		// in.close();
		//
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
		//
		// assertEquals(NODESIZES[LITAUEN], graph.noOfNodes());

		System.gc();

		System.out.println("used Memory: "
				+ (RUNTIME.totalMemory() - RUNTIME.freeMemory()) + " byte");

		graph = null;
		System.gc();

		System.out.println("\n");
	}

	@Test
	public void testUpdateableGraphDACH() {

		UpdateableGraph graph = GraphIO.importUpdatableGraph("D:\\sparseDACH",
				"", StreetProfile
						.getInstance(StreetProfile.DEFAULT_PROFILE),
				Constants.DISTANCE_OPT);

		System.out.println(graph.noOfNodes() + " nodes");
		System.out.println(graph.noOfEdges() + " edges");

		assertEquals(NODESIZES[DACH], graph.noOfNodes());

		// try {
		//
		// File file = new File("D:\\DACH.upg");
		// if (!file.exists())
		// file.createNewFile();
		// FileOutputStream out = new FileOutputStream(file);
		// graph.serialize(out);
		// out.close();
		//
		// graph = null;
		// System.gc();
		//
		// FileInputStream in = new FileInputStream(file);
		// graph = UpdateableGraph.deserialize(in);
		// in.close();
		//
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
		//
		// assertEquals(NODESIZES[DACH], graph.noOfNodes());

		System.gc();

		System.out.println("used Memory: "
				+ (RUNTIME.totalMemory() - RUNTIME.freeMemory()) + " byte");

		graph = null;
		System.gc();

	}

}
